﻿
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Vehicle.Web.Pages.Motorcade
{
    /// <summary>
    /// Copyright (C) M-Tear团队
    /// 文 件 名：Motorcade_Edit.aspx.cs
    /// 版    本：v1.00.0000
    /// 创建标识：2009-08-29   Created by 杨光远 V1.00.0000
    /// 功能说明：编辑车队信息页面类
    /// 注意事项：无
    /// 
    /// 更新记录：
    /// 
    /// </summary>
    public partial class Motorcade_Edit : Component.PageBase
    {
        #region ---基本变量实例化---
        Vehicle.BLL.Motorcade b_Motorcade = new Vehicle.BLL.Motorcade();//定义逻辑层变量
         BLL.Log b_Log = new Vehicle.BLL.Log();
        Model.MotorcadeInfo m_MotorcadeInfo = new Vehicle.Model.MotorcadeInfo();//实例化实体层
        #endregion

        #region ---基本方法---
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                MotorcadeDataBind();//绑定数据
            }
        }
        /// <summary>
        /// 绑定车队方法
        /// </summary>
        public void MotorcadeDataBind()
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != "")
            {
                DataTable dt = b_Motorcade.GetModel(new Vehicle.Model.ComponentsInfo("Motorcade", "M_ID", Request.QueryString["id"].ToString(), "M_ID"));
                if (dt.Rows.Count > 0)
                {
                    txt_M_Name.Text = dt.Rows[0]["M_Name"].ToString();//绑定车队名称
                    txt_M_Note.Text = dt.Rows[0]["M_Note"].ToString();//绑定车队备注
                    txt_M_Num.Text = dt.Rows[0]["M_Num"].ToString();//绑定车队编号
                    txt_M_Time.Text = dt.Rows[0]["M_Time"].ToString();//绑定车队录入时间
                    hf_M_LastTime.Value = dt.Rows[0]["M_LastTime"].ToString();//绑定车队最后修改时间
                    hf_M_Num.Value = dt.Rows[0]["M_Num"].ToString();//绑定车队编号
                }
                else
                {
                    Common.MessageHelper.AlertBack("该记录已删除!");
                }
            }
            else
            {
                Common.MessageHelper.AlertBack("您的操作有误,请重新尝试!");
            }
        }
        /// <summary>
        /// 修改数据逻辑判断是否有数据更新事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Add_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["id"] != null && Request.QueryString["id"] != "")
            {
                if (hf_Set.Value == "1")//取值判断
                {
                    //提交前判断数据是否有被改变
                    if (b_Motorcade.GetModel(new Vehicle.Model.ComponentsInfo("Motorcade", "M_ID", Request.QueryString["id"].ToString(), "M_ID")).Rows[0]["M_LastTime"].ToString() == hf_M_LastTime.Value)
                    {
                        Repeat();
                    }
                    else
                    {
                        d_Show.Visible = true;
                        hf_Set.Value = "0";
                    }
                }
                else
                {
                    Repeat();
                }
            }
            else
            {
                Common.MessageHelper.AlertBack("您的操作有误,请重新尝试!");
            }
        }
        /// <summary>
        /// 逻辑判断是否有编号重复
        /// </summary>
        public void Repeat()
        {
            //判断当前输入的编号是否和之前的编号相同,不相同则先判断是否有重复的编号
            if (txt_M_Num.Text.Trim() != hf_M_Num.Value.ToString())
            {
                //添加前判断是否已经有该编号的车队
                if (b_Motorcade.GetModel(new Vehicle.Model.ComponentsInfo("Motorcade", "M_Num", txt_M_Num.Text.Trim(), "M_ID")).Rows.Count == 0)
                {
                    UpdateDate();
                }
                else
                {
                    Common.MessageHelper.Alert("已经有相同该编号的车队,请重新填写");
                }
            }
            else
            {
                UpdateDate();
            }
        }

        /// <summary>
        /// 修改数据
        /// </summary>
        protected void UpdateDate()
        {
            m_MotorcadeInfo.M_ID = Common.TextHelper.StringToInt(Request.QueryString["id"].ToString());//车队ID
            m_MotorcadeInfo.M_Name = txt_M_Name.Text.Trim();//车队名称
            m_MotorcadeInfo.M_Time = Common.TextHelper.StringToDateTime(txt_M_Time.Text);//车队录入时间 默认为当前时间
            m_MotorcadeInfo.M_Num = txt_M_Num.Text.Trim();//车队编号
            m_MotorcadeInfo.M_Note = Common.TextHelper.CheckStr(txt_M_Note.Text);//车队备注 添加时进行安全过滤
            m_MotorcadeInfo.M_LastTime = DateTime.Now;//车队信息最后修改时间 默认为当前时间
            m_MotorcadeInfo.M_IsDelete = 0;//车队资料是否进行删除 默认为0 则不删除
            b_Motorcade.Update(m_MotorcadeInfo);
            b_Log.Add(new Vehicle.Model.LogInfo(session_ID, DateTime.Now, "成功修改编号为" + txt_M_Num.Text.Trim() + "的车队信息"));//添加日志
            Common.MessageHelper.AlertRedirect("车队信息修改成功", "Main.aspx");
        }
        #endregion
    }
}
